<?php
	/**
	 * RMB换金币
	 */
    class ExchangeMoney extends APIBase {
		
		public $tag = "ExchangeMoney";
		
		public function before() {
			$this->initMysql();
			$this->initCacheRedis();
			return true;
		}
		
		public function logic() {
			if (!isset($this->param['goods_id'])) {
				$this->returnError(300, 'goods_id is needed');
			}

			if (!isset($this->param['pay_type'])) {
				//$this->returnError(300, 'pay_type is needed');
			}

			if (!isset($this->param['vercode'])) {
				$this->returnError(300, 'vercode is needed');
				//$vercode = " ";
			}else if(strlen($this->param['vercode']) > 30){
				$vercode = substr($this->param['vercode'],0,30);
			}else{
				$vercode = $this->param['vercode'];
			}

			if (!isset($this->param['ua'])) {
				$this->returnError(300, 'ua is needed');
				//$ua = " ";
			}else if(strlen($this->param['ua']) > 255){
				$ua = substr($this->param['ua'],0,255);
			}else{
				$ua = $this->param['ua'];
			}

			if (!isset($this->param['imsi'])) {
				$this->returnError(300, 'imsi is needed');
				//$imsi = " ";
			}else if(strlen($this->param['imsi']) > 50){
				$imsi = substr($this->param['imsi'],0,50);
			}else{
				$imsi = $this->param['imsi'];
			}

			if (!isset($this->param['imei'])) {
				$this->returnError(300, 'imei is needed');
				//$imei = " ";
			}else if(strlen($this->param['imei']) > 50){
				$imei = substr($this->param['imei'],0,50);
			}else{
				$imei = $this->param['imei'];
			}

			if (!isset($this->param['cid'])) {
				$this->returnError(300, 'cid is needed');
				//$cid = " ";
			}else if(strlen($this->param['cid']) > 50){
				$cid = substr($this->param['cid'],0,50);
			}else{
				$cid = $this->param['cid'];
			}

			if (!isset($this->param['channel'])) {
				//$this->returnError(300, 'channel is needed');
				$channel = "0000000000";
			}else if(strlen($this->param['channel']) > 50){
				$channel = substr($this->param['channel'],0,50);
			}else{
				$channel = $this->param['channel'];
			}

			$goods = $this->cache_redis->hGetAll("hg:{$this->param['goods_id']}");
			if (!$goods) {
				$this->returnError(321, 'goods_id is wrong');
			}
			$cur_rmbs = $this->data_redis->hincrBY("hu:{$this->uid}", "rmb", $goods['rmb']);
			$money = $this->data_redis->hMGet("hu:{$this->uid}", array("money", "rmb", "coin"));
			
			if ($money['rmb'] < $goods['rmb'])	{
				$this->returnError(322, 'rmb is too least, please pay');
				return;
			}
			
			//扣除人民币
			//echo $goods['rmb'];	
			$cur_rmb = $this->data_redis->hincrBY("hu:{$this->uid}", "rmb", 0 - $goods['rmb']);
			$add_money = $goods['money'] + $goods['give_money'];
			$cur_money = $this->data_redis->hincrBY("hu:{$this->uid}","money", $add_money);
			$countpayrmb = $this->data_redis->hincrBY("hu:{$this->uid}","countpayrmb", $goods['rmb']);
			$paycount = $this->data_redis->hincrBY("hu:{$this->uid}","pay_count", 1);
			$cur_coin = $this->data_redis->hincrBY("hu:{$this->uid}","coin", $goods['coin']);
			$this->addGoodsLog($this->uid, $this->param['goods_id'], $goods['rmb'], $goods['money'], $goods['give_money']);
			
			$money = $this->data_redis->hMGet("hu:{$this->uid}", array("money", "rmb", "coin"));
			$money['money'] = (int)$money['money'];
			$money['rmb'] = (int)$money['rmb'];
			$money['coin'] = (int)$money['coin'];
			
			$now = time();
			$record = array(
				'uid' => $this->uid,
				'rmb' => $goods['rmb'],
				'create_time' =>$now,
				'vercode' => $vercode,
				'ua'	  => $ua,
				'imsi'	  => $imsi,
				'imei'    => $imei,
				'cid'     => $cid,
				'channel' => $channel
			);
			 
			
			if ($this->param['pay_type'] == 1){
				$this->mysql->insert("payrecord", $record);
			}
			$this->returnData($money);
		}
		
        public function after() {
    		$this->deinitDataRedis();
			$this->deinitCacheRedis();
			$this->deinitMysql();
		}
    }
?>
